package com.wdy.service.impl;

import java.util.LinkedList;
import java.util.List;

import com.wdy.dao.PostDao;
import com.wdy.po.Post;
import com.wdy.service.PostManager;
import com.wdy.util.PageUtil;
import com.wdy.vo.Condition;
import com.wdy.vo.Page;
import com.wdy.vo.Result;

@SuppressWarnings("unchecked")
public class PostManagerImpl implements PostManager {

	private PostDao postDao;

	public void setPostDao(PostDao postDao) {
		this.postDao = postDao;
	}

	public void savePost(Post post) {
		postDao.save(post);
	}

	public void removePost(Post post) {
		postDao.delete(post);
	}

	public void updatePost(Post post) {
		postDao.attachDirty(post);
	}

	public List<Post> listAll() {
		return postDao.findAll();
	}

	public List<Post> findByDate(String value) {
		return postDao.findByDate(value);
	}

	@Override
	public Result query(List<Condition> conList, Page page) {
		// TODO Auto-generated method stub
		int totalRecords = postDao.getQueryCount(conList);
		page = PageUtil.createPage(page, totalRecords);
		List tempList = (List) postDao.query(conList, page);
		List<Post> posts = new LinkedList<Post>();
		for (Object o : tempList) {
			if (o instanceof Object[]) {
				Object[] oList = (Object[]) o;
				for (Object oo : oList) {
					if (oo instanceof Post) {
						posts.add((Post) oo);
					}
				}
			} else {
				if (o instanceof Post) {
					posts.add((Post) o);
				}
			}
		}
		return new Result(page, posts);
	}

	@Override
	public void removePosts(Object[] objects) {
		// TODO Auto-generated method stub
		postDao.deleteBatch(objects);
	}

}
